package com.shengde.jinwawaforleader.model.homePage;

import android.os.Bundle;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.widget.TextView;

import com.google.gson.Gson;
import com.shengde.jinwawaforleader.R;
import com.shengde.jinwawaforleader.base.BaseActivity;
import com.shengde.jinwawaforleader.bean.User;
import com.shengde.jinwawaforleader.model.homePage.adapter.HappyListAdapter;
import com.shengde.jinwawaforleader.model.homePage.adapter.RecyclerViewDivider;
import com.shengde.jinwawaforleader.network.BasexUtilsProtocol;
import com.shengde.jinwawaforleader.network.protocolbean.ProHappyListInfo;

import org.xutils.common.Callback;
import org.xutils.http.RequestParams;
import org.xutils.x;

import java.util.ArrayList;
import java.util.List;

/**
 * 请假人数
 * Created by Chason on 2016/12/22.
 */

public class HappyListActivity extends BaseActivity{

    private TextView tv_back,tv_title;

    private RecyclerView rcv;
    private HappyListAdapter adapter;
    private List<ProHappyListInfo.StuInfoEntity> stuInfo = new ArrayList<>();
    private List<ProHappyListInfo.InfoEntity> info = new ArrayList<>();

    @Override
    public void onRoot(Bundle savedInstanceState) {
        setContentView(R.layout.activity_happylist);
    }

    @Override
    public void initView() {
        tv_back = (TextView)findViewById(R.id.tv_back);
        tv_title = (TextView)findViewById(R.id.tv_title);
        rcv = (RecyclerView)findViewById(R.id.rcv);
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this,LinearLayoutManager.VERTICAL,false);
        rcv.setLayoutManager(linearLayoutManager);
        //添加分割线
        //设置Item增加、移除动画
        rcv.setItemAnimator(new DefaultItemAnimator());
        rcv.addItemDecoration(new RecyclerViewDivider(this,LinearLayoutManager.VERTICAL,1, ContextCompat.getColor(this,R.color.divider_list)));
        adapter = new HappyListAdapter(this,stuInfo,info);
        rcv.setAdapter(adapter);
    }

    @Override
    public void initData() {
        tv_title.setText("请假列表");
        postLeaveInfoRequest();
    }

    @Override
    public void initListener() {
        tv_back.setOnClickListener(this);
    }

    @Override
    public void onWidgetClick(View view) {
        switch (view.getId()) {
            case R.id.tv_back:
                finish();
                break;
        }
    }

    /**
     * 学校所有请假信息
     */
    public void postLeaveInfoRequest(){
        RequestParams params = BasexUtilsProtocol.sendHttpRequest(this,"/kaoqin/leaveInfo");
        params.addParameter("schoolid", User.getSchoolId(this));
        x.http().post(params, new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String result) {
                ProHappyListInfo info = new Gson().fromJson(result, ProHappyListInfo.class);
                if(info!=null&&info.getCode()==0) {
                    Log.e("TAG", "info.getStuInfo().size()="+info.getStuInfo().size());
                    fillingData(info);
                }
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
            }

            @Override
            public void onCancelled(CancelledException cex) {
            }

            @Override
            public void onFinished() {
                BasexUtilsProtocol.dialog.cancel();
            }
        });
    }

    /**
     * 装配数据
     * @param info
     */
    public void fillingData(ProHappyListInfo info){
        adapter.resetList(info.getStuInfo(),info.getInfo());
        adapter.notifyDataSetChanged();
    }
}